<?php
App::uses('AppModel', 'Model');
/**
 * Exemplaire Model
 *
 * @property Edition $Edition
 * @property Utilisateur $Utilisateur
 * @property Demande $Demande
 */
class Exemplaire extends AppModel {

/**
 * Display field
 *
 * @var string
 */
	public $displayField = 'id';

/**
 * Validation rules
 *
 * @var array
 */
	public $validate = array(
		'edition_id' => array(
			'numeric' => array(
				'rule' => array('numeric'),
				//'message' => 'Your custom message here',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
		),
		'utilisateur_id' => array(
			'numeric' => array(
				'rule' => array('numeric'),
				//'message' => 'Your custom message here',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
		),
		'note' => array(
			'numeric' => array(
				'rule' => array('numeric'),
				//'message' => 'Your custom message here',
				'allowEmpty' => true,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
		),
	);

	//The Associations below have been created with all possible keys, those that are not needed can be removed

/**
 * belongsTo associations
 *
 * @var array
 */
	public $belongsTo = array(
		'Edition' => array(
			'className' => 'Edition',
			'foreignKey' => 'edition_id',
			'conditions' => '',
			'fields' => '',
			'order' => ''
		),
		'Utilisateur' => array(
			'className' => 'Utilisateur',
			'foreignKey' => 'utilisateur_id',
			'conditions' => '',
			'fields' => '',
			'order' => ''
		)
	);

/**
 * hasMany associations
 *
 * @var array
 */
	public $hasMany = array(
		'Demande' => array(
			'className' => 'Demande',
			'foreignKey' => 'exemplaire_id',
			'dependent' => false,
			'conditions' => '',
			'fields' => '',
			'order' => '',
			'limit' => '',
			'offset' => '',
			'exclusive' => '',
			'finderQuery' => '',
			'counterQuery' => ''
		)
	);
	
	public function afterFind($results, $primary = false) {
		foreach ($results as $key => $val) {
		
			if (isset($val['ExemplaireUtilisateur']['possede'])) {
				$results[$key]['ExemplaireUtilisateur']['est_possede'] = $this->estPossede($val['ExemplaireUtilisateur']['possede']) ;
			}				
			
			if (isset($val['ExemplaireUtilisateur']['lu'])) {
				$results[$key]['ExemplaireUtilisateur']['est_lu'] = $this->estPossede($val['ExemplaireUtilisateur']['lu']) ;
			}			
			if (isset($val['Exemplaire']['possede'])) {
				$results[$key]['Exemplaire']['est_possede'] = $this->estPossede($val['Exemplaire']['possede']) ;
			}
			
			if (isset($val['Exemplaire']['lu'])) {
				$results[$key]['Exemplaire']['est_lu'] = $this->estPossede($val['Exemplaire']['lu']) ;
			}			
		}
		return $results;
	}
	
	public function estPossede($possede) {
		return $possede=="1";
	}
	
	public function estLu($lu) {
		return $lu=="1";
	}	
}
